package data

import (
	"gitee.com/shuokeyun/merchant-service/internal/data/ent"
	"gitee.com/shuokeyun/merchant-service/internal/domain"
	"google.golang.org/protobuf/types/known/timestamppb"
	"time"
)

func userPoConvDo(do *ent.MerchantUser) *domain.MerchantUser {
	if do == nil {
		return nil
	}
	return &domain.MerchantUser{
		Id:         int32(do.ID),
		MerchantId: do.MerchantID,
		Mobile:     do.Mobile,
		Username:   do.Username,
		RealName:   do.RealName,
		Password:   do.Password,
		Salt:       do.Salt,
		Status:     do.Status,
		Detail:     do.Detail,
		HeadImage:  do.HeadImage,
		Role:       do.Role,
		CreatedAt:  do.CreatedAt,
		UpdatedAt:  do.UpdatedAt,
	}
}

func merchantPoConvDo(do *ent.Merchant, cdo *ent.MerchantCertification) *domain.Merchant {
	if do == nil {
		return nil
	}
	var certification *domain.MerchantCertification
	if cdo != nil {
		formatDate := func(t time.Time) string {
			if t.IsZero() {
				return ""
			}
			return t.Format("2006-01-02")
		}
		certification = &domain.MerchantCertification{
			Id:                           int32(cdo.ID),
			LegalPerson:                  cdo.LegalPerson,
			LegalPersonCardId:            cdo.LegalPersonCardID,
			LegalPersonIdentityFrontSide: cdo.LegalPersonIdentityFrontSide,
			LegalPersonIdentityBackSide:  cdo.LegalPersonIdentityBackSide,
			BusinessLicense:              cdo.BusinessLicense,
			BusinessLicensePhoto:         cdo.BusinessLicensePhoto,
			BusinessLicenseStartDate:     formatDate(cdo.BusinessLicenseStartDate),
			BusinessLicenseEndDate:       formatDate(cdo.BusinessLicenseEndDate),
			MedicalLicense:               cdo.MedicalLicense,
			MedicalLicenseName:           cdo.MedicalLicenseName,
			MedicalLicensePhoto:          cdo.MedicalLicensePhoto,
			MedicalLicenseStartDate:      formatDate(cdo.MedicalLicenseStartDate),
			MedicalLicenseEndDate:        formatDate(cdo.MedicalLicenseEndDate),
			IsNonprofit:                  int32(cdo.IsNonprofit),
			Property:                     int32(cdo.Property),
			DrugLicense:                  cdo.DrugLicense,
			DrugLicensePhoto:             cdo.DrugLicensePhoto,
			ContractPhoto:                cdo.ContractPhoto,
			ContractStartDate:            formatDate(cdo.ContractStartDate),
			ContractEndDate:              formatDate(cdo.ContractEndDate),
			CreatedAt:                    timestamppb.New(cdo.UpdatedAt),
			UpdatedAt:                    timestamppb.New(cdo.UpdatedAt),
		}
	}
	return &domain.Merchant{
		Id:                int32(do.ID),
		Name:              do.Name,
		PriceArea:         do.PriceArea,
		SalesmanId:        do.SalesmanID,
		Discount:          int32(do.Discount),
		ContractDiscount:  int32(do.ContractDiscount),
		SettleType:        int32(do.SettleType),
		Introduction:      do.Introduction,
		ProvinceCode:      do.ProvinceCode,
		CityCode:          do.CityCode,
		AreaCode:          do.AreaCode,
		Address:           do.Address,
		Lng:               do.Lng,
		Lat:               do.Lat,
		Status:            int32(do.Status),
		InstitutionalType: int32(do.InstitutionalType),
		MainBusiness:      do.MainBusiness,
		CreatedAt:         timestamppb.New(do.CreatedAt),
		UpdatedAt:         timestamppb.New(do.UpdatedAt),
		Certification:     certification,
	}
}
